package com.xf.lastmall.repository;


import com.xf.lastmall.moudle.entity.OrderDetailEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface OrderDetailRepository extends JpaRepository<OrderDetailEntity, String> {

    List<OrderDetailEntity> findByOrderNo(String orderNo);

    /**
     * 增加退款数量
     *
     * @param orderSubNo 订单号
     * @param refundNum  退款数量
     * @return rows
     */
    @Modifying
    @Query(value = "UPDATE tb_order_detail SET refund_quality_= refund_quality_+?2,update_time_=now() " +
            "WHERE order_sub_no_=?1 " +
            "AND quality_-consume_quality_-refund_quality_-expired_quantity_>=?2", nativeQuery = true)
    int updateRefundNum(String orderSubNo, int refundNum);


    /**
     * 回滚退款数
     *
     * @param orderSubNo 订单号
     * @param refundNum  退款数量
     * @return rows
     */
    @Query(value = "UPDATE tb_order_detail SET refund_quality_= refund_quality_-?2,update_time_=now() " +
            "WHERE order_sub_no_=?1 AND refund_quality_>=?2", nativeQuery = true)
    int revertRefundNum(String orderSubNo, int refundNum);

}
